<!--/**
*用户管理*
*
* @author LWG
* @date 2022/7/31 15:25
* @since 1.0.0
*/-->

<template>

  <div>
    <div>
      <b style="font-size: 40px">用户管理</b>
      <br><br>
      <b style="font-size: 20px">全部用户</b>
    </div>
    <hr>

    <div style="padding: 10px 0">
      <el-input style="width: 200px;font-size: 20px" placeholder="请输入用户名" v-model="username" suffix-icon="el-icon-search"></el-input>
      <el-button style="font-size: 20px" class="ml-5" type="primary" @click="loadForName">搜索</el-button>
      <el-button style="font-size: 20px" type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button style="font-size: 15px" type="primary" @click="download">导出所有用户数据<i class="el-icon-download"></i></el-button>
    </div>

    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" 
              :row-style="{height:'40px'}"
              :cell-style="{padding:'0px'}"
              style="width: 100%;font-size: 20px"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="用户id" width="120">
      </el-table-column>
      <el-table-column prop="name" label="用户名" width="120">
      </el-table-column>
      <el-table-column prop="account" label="手机号/邮箱" width="200">
      </el-table-column>
      <el-table-column label="注册日期" width="200">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="state" label="用户状态" width="190">
      </el-table-column>
      <el-table-column prop="address" label="地址" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="success" @click="changeState(scope.row.id)"
            v-show="scope.row.state == 'normal' ? true : false">正常<i class="el-icon-circle-check"></i>
          </el-button>
          <el-button type="danger" @click="changeState(scope.row.id)" v-show="scope.row.state == 'normal' ? false : true">
            禁用<i class="el-icon-circle-close"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button style="font-size: 15px" @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>
      <el-button style="font-size: 20px" @click="toggleSelection()">取消选择</el-button>
    </div>

    <div style="padding: 10px ">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes=[10,20,30,40]
          :page-size=pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
  </div>
</template>

<script>

export default {
  name: "User",
  //   data() {
  //     return {
  //       tableData: [],
  //       total: 0,
  //       sideWidth: 200,
  //       username: "",
  //       dialogFormVisible: false,
  //       form: {},
  //       pageNum: 0,
  //       pageSize: 10
  //     }
  //   },
  data() {
    return {
      tableData: [{
        id: 1,
        name: '段三',
        account: '18388876032',
        date: '2016-05-03',
        state: 'normal',
        userLog: '',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        id: 2,
        name: '小锥子',
        account: '18988411276',
        date: '2016-05-03',
        state: 'normal',
        userLog: '',
        address: '临沧市凤庆县 1518 弄'
      }, {
        id: 3,
        name: '鸭蛋',
        account: '18388876032',
        date: '2016-05-03',
        state: 'UN_normal',
        userLog: '',
        address: '临沧市凤庆县盘银镇 1518 弄'
      }, {
        id: 4,
        name: '蛋兄',
        account: '18988411276',
        date: '2016-05-03',
        state: 'normal',
        userLog: '',
        address: '临沧市凤庆县盘银镇段家组 1518 弄'
      }, {
        id: 5,
        name: '小锥锥',
        account: '469220494@qq.com',
        date: '2016-05-03',
        state: 'UN_normal',
        userLog: '',
        address: '云南省临沧市凤庆县盘银镇 1518 弄'
      }, {
        id: 6,
        name: '段老奶',
        account: '18388876032',
        date: '2016-05-03',
        state: 'normal',
        userLog: '',
        address: '临沧市临翔区 1518 弄'
      }, {
        id: 7,
        name: '将军',
        account: '469220494@qq.com',
        date: '2016-05-03',
        state: 'UN_normal',
        userLog: '',
        address: '内蒙古呼伦贝尔市 1518 弄'
      }],

      total: 0,
      sideWidth: 200,
      username: "",
      dialogFormVisible: false,
      form: {},
      pageNum: 0,
      pageSize: 10,
      multipleSelection: []
    }
  },

  created() {
    //请求分页查询数据
    this.load()
  },

  methods: {
    handleSizeChange(pageSize) {//设置每页展示多少条数据
      // console.log("设置每页展示多少条数据" + pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {//设置第几页
      // console.log("设置第几页" + pageNum)
      this.pageNum = pageNum
      this.load()
    },
    load() {//发起分页查询的网络请求
      this.request.get("admin/selectUser/" + this.pageNum + "/" + this.pageSize + "?username=").then(res => {
        // console.log(res)
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    loadForName() {//发起分页查询的网络请求
      this.request.get("admin/selectUser/" + this.pageNum + "/" + this.pageSize + "?username=" + this.username).then(res => {
        // console.log(res)
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    reset() {
      //将搜索框清空
      this.username = ""
      //刷新页面
      this.load()
    },
    changeState(id) {
      //将用户登录状态取反
      this.request.get("admin/changeState/" + id).then(res => {
        // console.log(res)
        if (res.state == 200) {
          //修改成功,刷新页面
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message);
        }
        this.load()
      })
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    download() {//调用系统对象，通过浏览器对象打开下面的网址下载用户数据
      // window.open('http://10.1.188.121:8160/admin/export');
      window.open('http://localhost:8160/admin/export');
      // window.open('http://adastra.isamumu.cn:8160/admin/export');
    }
  }
}
</script>

<style scoped>
</style>
